package com.munhrasoft.elearning.business.util;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.ejb.embeddable.EJBContainer;
import javax.naming.Context;

import com.munhrasoft.elearning.business.entities.CourseClass;
import com.munhrasoft.elearning.business.entities.Courses;
import com.munhrasoft.elearning.business.entities.Exercises;
import com.munhrasoft.elearning.business.entities.Questions;
import com.munhrasoft.elearning.business.entities.Students;
import com.munhrasoft.elearning.business.logic.CoursesEJB;
import com.munhrasoft.elearning.business.logic.GenericEJB;
import com.munhrasoft.elearning.business.logic.StudentsEJB;

public class Populate {

    private static Logger                logger = Logger.getLogger(Populate.class.getName());

    private static CoursesEJB            coursesEJB;
    private static GenericEJB<Students>  genericEJB;
    private static GenericEJB<Questions> genericQstEJB;
    private static StudentsEJB           studentsEJB;
    private static EJBContainer          ec;
    private static Context               ctx;

    public static void main(String[] args) {
        try {
            initContainer();
            closeContainer();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void initContainer() throws Exception {
        System.out.println("initContainer");
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put(EJBContainer.MODULES, new File("target/classes"));
        properties.put("org.glassfish.ejb.embedded.glassfish.installation.root",
                "./src/test/glassfish");
        ec = EJBContainer.createEJBContainer(properties);
        ctx = ec.getContext();
        genericEJB = (GenericEJB<Students>) ctx.lookup("java:global/classes/GenericEJB");
        coursesEJB = (CoursesEJB) ctx.lookup("java:global/classes/CoursesEJB");
        studentsEJB = (StudentsEJB) ctx.lookup("java:global/classes/StudentsEJB");
        genericQstEJB = (GenericEJB<Questions>) ctx.lookup("java:global/classes/GenericEJB");
    }

    public static void closeContainer() throws Exception {
        ec.close();
        System.out.println("AfterClass");
    }
    
    private static Students createExercises(Students student){
        
        
        //exercises
        Exercises jsfexercises1 = new Exercises();
        Exercises jsfexercises2 = new Exercises();
        Exercises jsfexercises3 = new Exercises();
        
        jsfexercises1.setTitle("What is the tag used for show a text");
        jsfexercises2.setTitle("What is the tag used to show a table");
        jsfexercises3.setTitle("What is the tag used to show messages");
        
        Exercises jpaexercises1 = new Exercises();
        Exercises jpaexercises2 = new Exercises();
        Exercises jpaexercises3 = new Exercises();

        jpaexercises1.setTitle("What is the name of the required xml for jpa configuration");
        jpaexercises2.setTitle("What is the annotation to mark a many to one relation");
        jpaexercises3.setTitle("Witch one below is a JPA implementation");
        
        //questions exerc1 jsf
        Questions jsfExerc1Question1 = new Questions();
        Questions jsfExerc1Question2 = new Questions();
        Questions jsfExerc1Question3 = new Questions();
        Questions jsfExerc1Question4 = new Questions();
        Questions jsfExerc1Question5 = new Questions();
        
        //questions exerc2 jsf
        Questions jsfExerc2Question1 = new Questions();
        Questions jsfExerc2Question2 = new Questions();
        Questions jsfExerc2Question3 = new Questions();
        Questions jsfExerc2Question4 = new Questions();
        Questions jsfExerc2Question5 = new Questions();
        
        //questions exerc3 jsf
        Questions jsfExerc3Question1 = new Questions();
        Questions jsfExerc3Question2 = new Questions();
        Questions jsfExerc3Question3 = new Questions();
        Questions jsfExerc3Question4 = new Questions();
        Questions jsfExerc3Question5 = new Questions();
        
        
        return student;
    }

    private static Students createCLasses(Students student) {

       CourseClass jsfclass1 = new CourseClass();
       CourseClass jsfclass2 = new CourseClass();
       CourseClass jsfclass3 = new CourseClass();
       
       jsfclass1.setTitle("Integrating Images with JSF by ProgrammingKnowledge");
       jsfclass2.setTitle("Configure Managed Beans in JSF by ProgrammingKnowledge");
       jsfclass3.setTitle("Simple Login Application by Calling Method in JSF Part-2 by ProgrammingKnowledge");
       
       jsfclass1.setVideourl("//www.youtube.com/embed/IMPP7CD_Gt8");
       jsfclass2.setVideourl("//www.youtube.com/embed/b1S2kFMMSd4");
       jsfclass3.setVideourl("//www.youtube.com/embed/vekS9l8ITmI");
           
       CourseClass jpaclass1 = new CourseClass();
       CourseClass jpaclass2 = new CourseClass();
       CourseClass jpaclass3 = new CourseClass();
       
       jpaclass1.setTitle("Create JPA Entities - Part-1");
       jpaclass2.setTitle("JPQL: The Basics of the Java Persistence Query Languages");
       jpaclass3.setTitle("Create JPA Entities - Part 3");
       
       
       List<CourseClass> jsfClassList = new ArrayList<CourseClass>();
       jsfClassList.add(jsfclass1);jsfClassList.add(jsfclass2);jsfClassList.add(jsfclass3);
       List<CourseClass> jpaClassList = new ArrayList<CourseClass>();
       jpaClassList.add(jpaclass1);jpaClassList.add(jpaclass2);jpaClassList.add(jpaclass3);
       
       student.getCourses().get(0).setClasses(jsfClassList);
       student.getCourses().get(1).setClasses(jpaClassList);
       
       return student;
    
    }

    private static Students createCourses(Students student) {

        Courses course1 = new Courses();
        course1.setTitle("Basic JSF course");
        Courses course2 = new Courses();
        course2.setTitle("Basic JPA course");

        List<Courses> coursesList = new ArrayList<Courses>();
        coursesList.add(course1);
        coursesList.add(course2);

        student.setCourses(coursesList);

        return student;

    }

    private static Students createStudent() {
        Students student = new Students();
        student.setName("Jonh O'Connor");
        return student;
    }

}
